/**
 * File: FilterQualityScore.java
 * Created by: mhaimel
 * Created on: 12 May 2009
 * CVS:  $Id: FilterQualityScore.java,v 1.1 2009/06/19 13:02:49 mhaimel Exp $
 */
package uk.ac.ebi.velvet;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import uk.ac.ebi.curtain.utils.data.FileType;
import uk.ac.ebi.curtain.utils.data.ReadType;
import uk.ac.ebi.curtain.utils.file.EntryMapper;
import uk.ac.ebi.curtain.utils.file.EntryMapperBean;
import uk.ac.ebi.curtain.utils.file.FileInfo;

/**
 * @author mhaimel
 *
 */
public class FilterQualityScore {
	private static Log log = LogFactory.getLog(FilterQualityScore.class);
	
	public static void main(String[] args) throws IOException {
//		log.info("Args: " + StringUtils.join(args,";"));
		List<String> fileArr = new ArrayList<String>();
		for(int i = 1; i < args.length; ++i){
			fileArr.add(args[i]);
		}
		Integer length = Integer.valueOf(args[0]);
		final BigInteger[] arr = new BigInteger[length];
		final Double[] arrAvg = new Double[length];
		final Long[][] bpCnt = new Long[5][length];
		final Long[] divCnt = new Long[1];

		for(String strFile : fileArr){
			divCnt[0] = 0L;
			for(int i = 0; i < bpCnt.length; ++i){
				for(int j = 0; j < bpCnt[i].length; ++j){
					bpCnt[i][j]=0l;;
				}
			}
			for(int i = 0; i < length; ++i ){
				arr[i]=BigInteger.valueOf(0l);
				arrAvg[i]=0d;
			}
			FileInfo fi = new FileInfo(
	//				"/homes/mhaimel/resources/velvetProd/input/helminth/solexa/paired_500-600/2466_1.paired.fastq.R",
	//				"/homes/mhaimel/resources/velvetProd/input/bongori/batch1/2212_3.fastq.F",
					strFile,
					FileType.fastq,ReadType.Short);
			fi.parse(new EntryMapper(){
				@Override
				public void mapEntry(EntryMapperBean bean) {
					++divCnt[0];
					processQuality(bean,arr);
					processSequence(bean,bpCnt);
				}

			});
			fillAvg(arr,arrAvg,divCnt);
			System.out.println(fi.getFile().getName()+"\tavg\t"+StringUtils.join(arrAvg, '\t'));
			System.out.println(fi.getFile().getName()+"\tA\t"+StringUtils.join(bpCnt[0], '\t'));
			System.out.println(fi.getFile().getName()+"\tT\t"+StringUtils.join(bpCnt[1], '\t'));
			System.out.println(fi.getFile().getName()+"\tG\t"+StringUtils.join(bpCnt[2], '\t'));
			System.out.println(fi.getFile().getName()+"\tC\t"+StringUtils.join(bpCnt[3], '\t'));
			System.out.println(fi.getFile().getName()+"\tN\t"+StringUtils.join(bpCnt[4], '\t'));
		}
	}
	
	private static void processQuality(EntryMapperBean bean, final BigInteger[] arr) {
		char[] charr = bean.getQuality().toCharArray();
		for(int i = 0; i < charr.length; ++i){
			char c = charr[i];
			arr[i] = arr[i].add(BigInteger.valueOf(((long)(((int)c)-33))));
		}
	}

	private static void processSequence(EntryMapperBean bean, Long[][] cnt) {
		char[] charr = bean.getSequence().toCharArray();
		for(int i = 0; i < charr.length; ++i){
			switch (charr[i]) {
			case 'a':
			case 'A':
				++cnt[0][i];
				break;
			case 't':
			case 'T':
				++cnt[1][i];
				break;
			case 'g':
			case 'G':
				++cnt[2][i];				
				break;
			case 'c':
			case 'C':
				++cnt[3][i];				
				break;
			default:
				++cnt[4][i];
				break;
			}
		}
	}
	
	private static void fillAvg(BigInteger[] arr, Double[] arrAvg,Long[] divCnt) {
		long cnt = divCnt[0];
		for(int i = 0; i < arr.length; ++i){
			long currB = arr[i].longValue();
			arrAvg[i] = Double.valueOf((double)currB/(double)cnt);
		}
	}


	private static BigInteger getMax(BigInteger ... bigArr){
		BigInteger max = bigArr[0];
		for(BigInteger big : bigArr){
			max = big.compareTo(max) > 0?big:max;
		}
		return max;
	}
}
